package com.fndsoft.police.xuancheng.ui.adapter.holder;

import java.util.Date;

import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.fndsoft.police.xuancheng.R;
import com.fndsoft.police.xuancheng.domain.ui.CommonFormRowDomain;
import com.fndsoft.police.xuancheng.handler.DialogHandler;
import com.fndsoft.police.xuancheng.handler.DialogHandler.MeapDialogListenerImpl;
import com.fndsoft.police.xuancheng.listener.CommonListener;
import com.fndsoft.police.xuancheng.ui.common.BigContentAc;
import com.fndsoft.police.xuancheng.util.DateUtil;
import com.fndsoft.police.xuancheng.util.ValueUtil;
import com.fndsoft.police.xuancheng.util.ViewUtil;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 通用列表行辅助
 * 
 * @author LiangXiangJu
 * @creationTime 2014-7-19 下午4:56:04
 * @version
 */
public class CommonFormRowHolder extends BaseHolder<CommonFormRowDomain> {

	public CommonFormRowHolder(View convertView, Object dataModel, boolean isRead) {
		super(convertView);
		ViewUtils.inject(this, convertView);
		this.dataModel = dataModel;
		this.isRead = isRead;
	}

	@ViewInject(R.id.tvTitle)
	TextView tvTitle;
	@ViewInject(R.id.tvText)
	TextView tvText;
	@ViewInject(R.id.etText)
	EditText etText;
	@ViewInject(R.id.spinner)
	TextView spinner;
	@ViewInject(R.id.ivArrowRight)
	ImageView ivArrowRight;
	private Object dataModel;
	private CommonFormRowDomain domain;
	private boolean isRead;

	private TextWatcher textWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			setFieldValue(s.toString());
		}
	};

	@Override
	public void hideAllView() {
		ViewUtil.setViewsGone(tvText, etText, spinner, ivArrowRight);
	}

	@Override
	public void fillDataByView(int index, final CommonFormRowDomain domain,
			View view) {
		this.domain = domain;
		// 标题
		tvTitle.setText(domain.getTitle());
		if (isRead) {
			ViewUtil.setViewsVisible(tvText);
			Object vlaue = getFieldValue();
			if (null != vlaue && (vlaue instanceof Date)) {
				tvText.setText(DateUtil.formatDate((Date) vlaue));
			} else {
				tvText.setText(getFieldStr());
			}

			// 大文本
			if (domain.getFieldType() == CommonFormRowDomain.EDIT_TEXT_BIG_TYPE) {
				ViewUtil.setViewsVisible(ivArrowRight);

				// 添加行点击事件
				domain.setCommonListener(new CommonListener() {

					@Override
					public void onClickOne(Object object) {
						BigContentAc.lauch(domain.getTitle(), tvText.getText()
								.toString(), false, null);
					}
				});
			}
		} else {
			switch (domain.getFieldType()) {
			case CommonFormRowDomain.EDIT_TEXT_TYPE: // 普通文本输入
				ViewUtil.setViewsVisible(etText);
				etText.setInputType(InputType.TYPE_CLASS_TEXT);
				etText.addTextChangedListener(textWatcher);
				etText.setText(getFieldStr());
				break;
			case CommonFormRowDomain.EDIT_NUMBER_TYPE: // 普通数字输入
				ViewUtil.setViewsVisible(etText);
				etText.setInputType(InputType.TYPE_CLASS_NUMBER);
				etText.addTextChangedListener(textWatcher);
				etText.setText(getFieldStr());
				break;
			case CommonFormRowDomain.EDIT_POP_LIST_TYPE: // 下拉框
				ViewUtil.setViewsVisible(spinner);

				// ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				// BaseAc.currentActivity,
				// android.R.layout.simple_spinner_item,
				// domain.getPopChooseValues());
				// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				// spinner.setAdapter(adapter);
				// spinner.setPrompt(domain.getTitle());
				// spinner.setOnItemSelectedListener(new
				// OnItemSelectedListener() {
				//
				// @Override
				// public void onItemSelected(AdapterView<?> arg0, View arg1,
				// int arg2, long arg3) {
				// setFieldValue(domain.getPopChooseValues()[arg2]);
				// }
				//
				// @Override
				// public void onNothingSelected(AdapterView<?> arg0) {
				//
				// }
				// });
				//
				// for (int i = 0; i < domain.getPopChooseValues().length; i++)
				// {
				// if (domain.getPopChooseValues()[i].equals(getFieldStr())) {
				// spinner.setSelection(i);
				// break;
				// }
				// }

				if (null == getFieldStr()) {
					spinner.setText(domain.getPopChooseValues()[0]);
					setFieldValue(domain.getPopChooseValues()[0]);
				} else {
					spinner.setText(getFieldStr());
				}

				break;
			case CommonFormRowDomain.EDIT_POP_DATE_TYPE: // 日期框
				ViewUtil.setViewsVisible(tvText);
				setFieldValue(getDateValue());
				tvText.setText(DateUtil.formatDate(getDateValue()));
				break;
			case CommonFormRowDomain.EDIT_TEXT_BIG_TYPE: // 大文本输入
				ViewUtil.setViewsVisible(tvText, ivArrowRight);
				tvText.setText(getFieldStr());
				break;
			default:
				ViewUtil.setViewsVisible(tvText);
				Object vlaue = getFieldValue();
				if (null != vlaue && (vlaue instanceof Date)) {
					tvText.setText(DateUtil.formatDate((Date) vlaue));
				} else {
					tvText.setText(getFieldStr());
				}
				break;
			}

			// 添加行点击事件
			domain.setCommonListener(new CommonListener() {

				@Override
				public void onClickOne(Object object) {
					switch (domain.getFieldType()) {
					case CommonFormRowDomain.EDIT_TEXT_BIG_TYPE: // 大文本输入
						BigContentAc.lauch(domain.getTitle(), tvText.getText()
								.toString(), true, new CommonListener() {
							@Override
							public void onClickOne(Object object) {
								// 大文本数据返回
								tvText.setText(String.valueOf(object));
								setFieldValue(String.valueOf(object));
							}
						});
						break;
					case CommonFormRowDomain.EDIT_POP_DATE_TYPE: // 日期框
						DialogHandler.showDateDialog(
								new MeapDialogListenerImpl() {
									@Override
									public void onSetDate(Date date) {
										setFieldValue(date);
										tvText.setText(DateUtil
												.formatDate(date));
									}
								}, getDateValue());
						break;
					case CommonFormRowDomain.EDIT_POP_LIST_TYPE: // 下拉列表（TextView实现）
						int index = 0;
						String valueResult = getFieldStr();
						if (null != valueResult) {
							for (int i = 0; i < domain.getPopChooseValues().length; i++) {
								String value = domain.getPopChooseValues()[i];
								if (valueResult.equals(value)) {
									index = i;
									break;
								}
							}
						}

						DialogHandler.showMulRowSingleChooseDialog(
								domain.getTitle(), domain.getPopChooseValues(),
								new MeapDialogListenerImpl() {
									@Override
									public void onClickSeletedIndex(
											int seletedIndex) {
										spinner.setText(domain
												.getPopChooseValues()[seletedIndex]);
										setFieldValue(domain
												.getPopChooseValues()[seletedIndex]);
									}
								}, index);
						break;
					default:
						break;
					}
				}
			});
		}
	}

	/**
	 * 实时获取最新日期属性值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-7-20 上午11:39:24
	 * @version
	 * @return
	 */
	private Date getDateValue() {
		Object dateObj = getFieldValue();
		Date date = null;
		if (null != dateObj && (dateObj instanceof Date)) {
			date = (Date) dateObj;
		}
		// 默认日期
		if (null == date) {
			date = new Date();
		}
		return date;
	}

	/**
	 * 读取属性值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-7-20 上午11:39:58
	 * @version
	 * @return
	 */
	private Object getFieldValue() {
		try {
			return ValueUtil.getReflectValue(dataModel, domain.getFieldName());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 读取属性值文本值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-7-20 上午11:39:58
	 * @version
	 * @return
	 */
	private String getFieldStr() {
		try {
			return ValueUtil.getStringValueOfObj(ValueUtil.getReflectValue(
					dataModel, domain.getFieldName()));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 设置属性值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-7-20 上午11:40:22
	 * @version
	 * @param obj
	 */
	private void setFieldValue(Object obj) {
		try {
			ValueUtil.setReflectValue(dataModel, domain.getFieldName(), obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
